//
//  HMTreasureCollectionController.m
//  HeartMark
//
//  Created by rimi on 2016/11/14.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMTreasureCollectionController.h"
#import "PCollectionViewLayout.h"
#import "HMLikeCollectionCell.h"
#import "HMLoginController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "HMLikeModel.h"
#import "HMTreasureDetailsViewController.h"
#import "HMAlertView.h"

@interface HMTreasureCollectionController ()<UICollectionViewDelegate,UICollectionViewDataSource,PCollectionViewLayoutDelegate>

@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)NSMutableArray* dataArray;

@end

@implementation HMTreasureCollectionController

//注册通知
- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"collectionNotification" object:nil];
        [self requestData];

    }
    return self;
}

//移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//通知方法
- (void)updateView:(NSNotification *)info {
    
    AVObject *obj = info.object;
    NSDictionary *userInfo = info.userInfo;
    
    NSDictionary* dict = @{@"content":obj[@"content"],@"updatedAt":obj[@"updatedAt"],@"author":obj[@"authorName"],@"collectNumber":obj[@"collectNumber"],@"authorId":obj[@"authorId"],@"noteObj":obj};
    HMLikeModel* likeModel = [[HMLikeModel alloc] initWithLikeModelDict:dict];
    
    if ([userInfo[@"isAdd"] isEqualToString:@"YES"]) {
       
        [self.dataArray insertObject:likeModel atIndex:0];
        
        
    } else {
        for (HMLikeModel *model in self.dataArray) {
            if ([model.noteObj isEqual:obj]) {
                [self.dataArray removeObject:model];
                break;
            }
        }
        
        
    }
    
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   [self.view addSubview:self.collectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    
  
        
    
   
    
}



//获取数据
-(void)requestData{
    //能进入这个界面首先肯定是登录过的 所以就不需要判断了
    // 假设 Tom 被保存到云端之后的 objectId 是 562da3fdddb2084a8a576d49
    //先获取当前用户的objectid
    AVUser* currentUser = [AVUser currentUser];
    
    // 读取 AVRelation 对象
    AVRelation *relation = [currentUser relationForKey:@"collectionNote"];
    
    // 获取关系查询
    AVQuery *query = [relation query];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        
        if (error) {
            [HMAlertView bottomAlertView:@"网络异常"];
        } else {
        
        for (AVObject* likeObject in objects) {
            
            NSDictionary* dict = @{@"content":likeObject[@"content"],@"updatedAt":likeObject[@"updatedAt"],@"author":likeObject[@"authorName"],@"collectNumber":likeObject[@"collectNumber"],@"authorId":likeObject[@"authorId"],@"noteObj":likeObject};
            HMLikeModel* model = [[HMLikeModel alloc]initWithLikeModelDict:dict];
            [self.dataArray insertObject:model atIndex:0];
        }
         
        }
    }];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.collectionView reloadData];
}

-(CGFloat)waterFlow:(PCollectionViewLayout *)flow heightForWidth:(CGFloat)width indexPath:(NSIndexPath *)index{
    HMLikeModel *cellModel = self.dataArray[index.item];
    CGFloat cellHeight = cellModel.cellHeight;
    
    return cellHeight;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HMLikeModel *model = self.dataArray[indexPath.item];
    HMTreasureDetailsViewController *TdVc = [[HMTreasureDetailsViewController alloc] init];
    TdVc.likeModel = model;
    
    [self.navigationController pushViewController:TdVc animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return  self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HMLikeCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.likeModel = self.dataArray[indexPath.item];
    cell.contentView.backgroundColor = [[UIColor alloc] initWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    return cell;
}

-(NSMutableArray*)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UICollectionView *)collectionView {
    if (!_collectionView) {
        //注册原形cell
        PCollectionViewLayout * layout = [[PCollectionViewLayout alloc]init];
        layout.delegate =self;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        
        [_collectionView registerClass:[HMLikeCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}

@end
